/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiof.moraa.server.dao;

import com.hiof.moraa.mapping.TblAthlete;
import com.hiof.moraa.mapping.TblSupport;
import com.hiof.moraa.mapping.TblSupportLiveMedia;
import com.hiof.moraa.server.service.model.LiveMediaFeed;
import com.hiof.moraa.server.service.model.SupportLiveMediaFeed;
import com.hiof.moraa.server.service.model.TypeOfAttendant;
import com.hiof.moraa.util.FileUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author manish
 */
public class HibernateSupportLiveMediaDAOImp implements SupportLiveMediaDAO {

    private SessionFactory factory;
    private SupportDAO supportDAO;

    public SessionFactory getFactory() {
        return factory;
    }

    public void setFactory(SessionFactory factory) {
        this.factory = factory;
    }

    public SupportDAO getSupportDAO() {
        return supportDAO;
    }

    public void setSupportDAO(SupportDAO supportDAO) {
        this.supportDAO = supportDAO;
    }

    @Override
    public SupportLiveMediaFeed insertSupportLiveMediaFeed(int supportId, String filePath, double lat, double lng, double height, Timestamp timestamp) {
        TblSupportLiveMedia supportLiveMedia = new TblSupportLiveMedia();
        try {
            TblSupport support = (TblSupport) factory.getCurrentSession().get(TblSupport.class, supportId);
            supportLiveMedia.setTblSupport(support);
            supportLiveMedia.setMediaType(FileUtils.computeMediaType(filePath));
            supportLiveMedia.setMediaPath(filePath);
            supportLiveMedia.setHeight(height);
            supportLiveMedia.setLat(lat);
            supportLiveMedia.setLng(lng);
            supportLiveMedia.setTimeStamp(timestamp);

            factory.getCurrentSession().save(supportLiveMedia);
        } catch (HibernateException e) {
            Logger.getLogger(HibernateSupportLiveMediaDAOImp.class.getName()).log(Level.SEVERE, "Hibernate Exception Occured", e);
            throw e;
        }

        SupportLiveMediaFeed liveMediaFeed = convert2LiveMediaFeed(supportLiveMedia);
        return liveMediaFeed;
    }

    @Override
    public List<SupportLiveMediaFeed> getAllMediaFeedBySupportId(int supportId) {
        Session session = factory.getCurrentSession();

        String hql = "from TblSupportLiveMedia media where media.tblSupport.id = :supportId order by media.timeStamp desc";
        Query query = session.createQuery(hql);
        query.setInteger("supportId", supportId);
        List<TblSupportLiveMedia> supportLiveMedias = query.list();

        List<SupportLiveMediaFeed> result = fetchToLiveMediaFeed(supportLiveMedias);
        supportLiveMedias.clear();
        return result;
    }

    @Override
    public List<SupportLiveMediaFeed> getLatestLiveMediaFeedOfAllSupportsInOneRoute(int routeId, int numOfFeed) {
        Session session = factory.getCurrentSession();

        String hql = "from TblSupportLiveMedia media where media.tblSupport.tblRoute.id = :routeId "
                + "order by media.timeStamp desc";
        Query query = session.createQuery(hql);
        query.setInteger("routeId", routeId);
        query.setMaxResults(numOfFeed);
        List<TblSupportLiveMedia> supportLiveMedias = query.list();

        List<SupportLiveMediaFeed> result = fetchToLiveMediaFeed(supportLiveMedias);
        supportLiveMedias.clear();
        return result;
    }

    @Override
    public List<SupportLiveMediaFeed> getLatestLiveMediaFeedOfOneSupportInOneRoute(String username, int routeId, int numOfLiveMediaFeed) {
        Session session = factory.getCurrentSession();

        String hql = "from TblSupportLiveMedia media where media.tblSupport.tblUser.username = :username and media.tblSupport.tblRoute.id = :routeId "
                + "order by media.timeStamp desc";
        Query query = session.createQuery(hql);
        query.setString("username", username);
        query.setInteger("routeId", routeId);
        query.setMaxResults(numOfLiveMediaFeed);
        List<TblSupportLiveMedia> supportLiveMedias = query.list();

        List<SupportLiveMediaFeed> result = fetchToLiveMediaFeed(supportLiveMedias);
        supportLiveMedias.clear();
        return result;
    }

    private List<SupportLiveMediaFeed> fetchToLiveMediaFeed(List<TblSupportLiveMedia> supportLiveMedias) {
        List<SupportLiveMediaFeed> result = new ArrayList<SupportLiveMediaFeed>();
        for (TblSupportLiveMedia media : supportLiveMedias) {
            result.add(convert2LiveMediaFeed(media));
        }
        return result;
    }

    private SupportLiveMediaFeed convert2LiveMediaFeed(TblSupportLiveMedia supportLiveMedia) {
        if (supportLiveMedia == null)
            return null;

        TblAthlete athlete = supportDAO.getAthlete(supportLiveMedia.getTblSupport().getId());
        SupportLiveMediaFeed liveMediaFeed = new SupportLiveMediaFeed(
                supportLiveMedia.getTblSupport().getTblUser().getId(),
                supportLiveMedia.getTblSupport().getTblUser().getUsername(),
                supportLiveMedia.getTblSupport().getTblUser().getFirstName() + " " + supportLiveMedia.getTblSupport().getTblUser().getLastName(),
                supportLiveMedia.getTblSupport().getTblUser().getProfilePicture(),
                supportLiveMedia.getTimeStamp(),
                supportLiveMedia.getTblSupport().getAttendantCode(),
                supportLiveMedia.getMediaPath(), supportLiveMedia.getLat(),
                supportLiveMedia.getLng(), supportLiveMedia.getHeight(), TypeOfAttendant.SUPPORT);

        liveMediaFeed.setAthleteId(athlete.getId());
        liveMediaFeed.setAthleteUsername(athlete.getTblUser().getUsername());
        liveMediaFeed.setAthleteAttendantCode(athlete.getAttendantCode());
        liveMediaFeed.setAthleteProfilePicture(athlete.getTblUser().getProfilePicture());
        liveMediaFeed.setAthleteFullName(athlete.getTblUser().getFirstName() + " " + athlete.getTblUser().getLastName());

        return liveMediaFeed;
    }
}
